<template>
  <div class=''>
    <el-dialog
      title="上传完成文件"
			:visible.sync="isShow"
      width="50%"
      center
      :before-close="handleClose">
        <!-- 完成文件 -->
        <div v-if="completeFile">
          <!-- <p>上传完成文件</p> -->
          <p style="color:red">* 文件格式：编号-姓名(发明人)-拟申报名称-专利类型.rar/zip 后缀名为.rar/.zip</p>
          <!-- <p style="color:red">* 拟申报名称必须和说明书中的申报名称一致。</p> -->
          <p style="color:red">* 专利类型必须为：发明专利，外观设计，实用新型。</p>
          <el-upload class="upload-demo" ref="uploadOk" :auto-upload="true" :on-change="handleChangeByok2" :action="baseUrl + 'api/order/'+ this.form.id +'/upload/confirm/file'"
            :file-list="fileListByok" :on-remove="handleRemove" :http-request="uploadSectionFile2" multiple :limit="5">
            <!-- <div slot="tip" class="el-upload__tip">选择的文件</div> -->
            <el-button v-no-more-click:8000 slot="trigger" size="small" type="primary">选择文件</el-button>
            <el-button v-no-more-click:8000 style="margin-left: 10px;" size="small" type="success" @click="sendCompleteFile">确认上传</el-button>
          </el-upload>
        </div>

    </el-dialog>

  </div>
</template>

<script>
import axios from "axios";
export default {
  data () {
    return {
      isShow: false,
      completeFile: false,
      reviewFile: false,
      fileListByok: [],
      fileListV: [],
      id: null,
      baseUrl: global.baseurl,
      form: {}
    }
  },
  methods: {
    show (data) {
      this.form = data
      if (data.status == '上报文件上传中') {
        this.completeFile = true
      } else if (data.status == '案件内审中' || data.status == '案件修改中') {
        this.reviewFile = true
      }
      this.id = data.id
      this.isShow = true
    },
    // 上传完成文件
    sendCompleteFile () {
      let list = document.getElementsByClassName(
        "el-upload-list__item is-ready"
      );
      if (list.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择需要上传的文件！"
        });
        return;
      }
      // FormData 对象
      var form = new FormData();
      // 文件对象
      for (let i in this.fileListV) {
        form.append(i==0?"file":("file"+(i*1+1)), this.fileListV[i].raw);
      }
      axios({
        method: "post",
        url: this.baseUrl + `/api/order/${this.id}/agent/order/confirm/file`,
        data: form,
        headers: {
          "Content-Type": "multipart/form-data",
          Authorization: "Bearer " + this.$store.state.token
        }
      }).then(res => {
        if (res.data.status == "success") {
          this.$message({
            type: "success",
            message: "上传成功"
          });
          this.$emit('refresh')
          this.isShow = false
        } else {
          this.$message({
            type: "error",
            message: res.data.message
          })
        }
      });
    },
    //完成文件
    handleChangeByok2(file, fileList) {

      let tempList = [];//可以上传的文件
      for(let i = 0;i<fileList.length;i ++){
        let canUpload = true;//是否可以上传
        let that = this;
        //验证文件类型
        let fileName = fileList[i].name;
        //文件切成数组 然后去判断格式
        let fileNameArr = fileName.substring(this.form.order_number.length + 1).split("-")
        console.log(this.form.order_number)
        let arrLen = fileName.split("-").length;
        let orderNum = fileName.substr(0, this.form.order_number.length); // 上传文件订单编号
        //获取后缀名 必须是压缩文件(zip/rar)
        console.log(orderNum)
        if(orderNum != this.form.order_number){
          that.$message.error("文件名的订单编号不正确");
          canUpload = false;
        }
        // else if(fileNameArr[0] == this.form.agent.name){
        //   that.$message.error("发明人姓名不正确");
        //   canUpload = false;
        // }
        else if(fileName.split(".")[1] !== "zip" && fileName.split(".")[1] !== "rar"){
          that.$message.error("文件必须为压缩包格式");
          canUpload = false;
        }else if(!/^(发明专利|实用新型|外观设计)$/.test(fileNameArr[fileNameArr.length-1].split(".")[0])){
          that.$message.error("专利类型必须是【发明专利, 实用新型, 外观设计】中的一种");
          canUpload = false;
        }else if (fileNameArr.length != 3) {
          that.$message.error(
            "请按照格式：(编号-姓名(发明人)-拟申报名称-专利类型.rar/zip),修改文件名后重新上传"
          )
          canUpload = false;
        }
        if(canUpload){
          tempList.push(fileList[i]);
        }
      }
      this.fileListV.splice(0,this.fileListV.length,...tempList);
      fileList.splice(0,fileList.length,...tempList);
      return ;
    },
    handleRemove(file, fileList) {
      this.fileListV = fileList;
    },
    uploadSectionFile2(params) {
      //this.fileListV.push(params.file);
    },
    handleClose () {
      this.form = {}
      this.isShow = false
    },
  }
}
</script>

<style lang='less' type='text/css'>
  .upload-demo {
    margin-top: 20px;
  }
</style>
